package com.leilei.serviceProject.trigger.job;

import com.leilei.serviceProject.domain.strategy.model.valobj.StrategyAwardCountSurplusVo;
import com.leilei.serviceProject.domain.strategy.service.IRaffleStock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author yanglei
 * @description 更新策略奖品库存
 * @create 2024/11/29 17:02
 */
@Slf4j
@Component
public class UpdateStrategyAwardStockJob {
    @Resource
    private IRaffleStock raffleStock;

    @Scheduled(cron = "0/5 * * * * ?")
    public void exec() {
        log.info("定时任务，更新奖品消耗库存【延迟队列获取，降低对数据库的更新频次，不要产生竞争】");
        try {
            StrategyAwardCountSurplusVo strategyAwardCountSurplusVo = raffleStock.takeQueueValue();
            if (null == strategyAwardCountSurplusVo) {
                return;
            }
            log.info("定时任务，更新奖品消耗库存 strategyId:{} awardId:{}", strategyAwardCountSurplusVo.getStrategyId(), strategyAwardCountSurplusVo.getAwardId());
            raffleStock.updateStrategyAwardStock(strategyAwardCountSurplusVo.getStrategyId(), strategyAwardCountSurplusVo.getAwardId());
        } catch (Exception e) {
            log.error("定时任务，更新奖品消耗库存失败", e);
        }
    }
}
